package com.example.jiankang.pages;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.jiankang.R;

public class dengluActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private SharedPreferences mSharedPreferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_denglu);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //获取mSharedPreferences
        mSharedPreferences = getSharedPreferences("user",MODE_PRIVATE);

        //初始化控件

        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        //点击注册
        TextView zhucetext = findViewById(R.id.text_zhuce);
        zhucetext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册页面
                Intent intent = new Intent(dengluActivity.this,zhuceActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.button_denglu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
                    Toast.makeText(dengluActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }else{
                    String name = mSharedPreferences.getString("username", null);
                    String pwd = mSharedPreferences.getString("password", null);
                    if (username.equals(name)&& password.equals(pwd)){

                        Intent intent = new Intent(dengluActivity.this, zhuyemianActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(dengluActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

    }
}